package com.ColonyCount.View;

import com.ColonyCount.Image.Image32;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

public class OverlayView extends View {
	
	Bitmap sourceBitmap;
	Image32 overlayImage;
	Rect overlayBounds;
	boolean overlayLocked = false;;
    int width;
    int height;
    
	public OverlayView(Context context) 
	{
		super(context);
	}
	
	public OverlayView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

	public void setSourceBitmap(Bitmap sourceBitmap)
	{
		this.sourceBitmap = sourceBitmap;
		invalidate();
	}

	public void setOverlayImage(Image32 overlayImage, Rect overlayBounds)
	{
		this.overlayImage = overlayImage;
		this.overlayBounds = overlayBounds;
		invalidate();
	}

	@Override
	protected void onDraw (Canvas canvas)
	{
		float scale = 1.0f;
    	if(sourceBitmap != null)
    	{
        	scale = (float)width/(float)sourceBitmap.getWidth();
        	canvas.scale(scale, scale);
        	
        	Paint paint = new Paint();
        	canvas.drawBitmap(sourceBitmap, 0, 0, paint);
    	}
    	
    	if(overlayImage != null)
    	{
        	int[] overlayPixels = overlayImage.getPixels();
        	int width = overlayImage.getWidth();
        	int height = overlayImage.getHeight();
        	Paint paint = new Paint();
        	canvas.drawBitmap(overlayPixels, 0, width, overlayBounds.left, overlayBounds.top, width, height, true, paint);
    	}    	
    	
  	   	if(sourceBitmap == null || overlayImage == null || overlayLocked)
    	{
    		Paint mPaintText = new Paint();
    		mPaintText.setStyle(Paint.Style.FILL);
    		mPaintText.setColor(Color.WHITE);
    		mPaintText.setTextSize(10/scale);
    		canvas.drawText("WORKING", width/2, height/2, mPaintText);
    	}
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		DisplayMetrics dm = getResources().getDisplayMetrics();
	    width = dm.widthPixels;
	    height = dm.heightPixels;
		setMeasuredDimension(width, width);
	}
	
    // Create runnable for posting
    final Runnable mWorkerThreadResults = new Runnable() {
        public void run() {
        	invalidate();
        }
    };
}
